<?php

namespace app\admin\controller;

use app\admin\logic\WorkRecordDataLogic;

/**
 * 每日数据明细api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/21
 * Time: 14:08
 */
class WorkRecordData extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->wrd_logic = new WorkRecordDataLogic();
    }

    /**
     * 获取每日数据明细
     * @return \think\response\Json
     */
    public function ajax_get_wrd()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取每日数据明细
        $r = $this->wrd_logic->get_wrd($params);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->wrd_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取每日数据明细详情
     * @return \think\response\Json
     */
    public function ajax_get_wrd_detail()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $wrd = new \app\common\controller\WorkRecordData();

        $maps = [];
        $maps[] = ['id', '=', $params['wrdid']];
        $wra_detail =  model('work_record_data')->get_detail($maps, '*');
        $wra_detail['front_money'] = deal_number_div_100($wra_detail['front_money']);
        $wra_detail['vip_back_money'] = deal_number_div_100($wra_detail['vip_back_money']);
        $wra_detail['check_in_back_money'] = deal_number_div_100($wra_detail['check_in_back_money']);
        $wra_detail['agency_back_money'] = deal_number_div_100($wra_detail['agency_back_money']);
        $wra_detail['account_back_money'] = deal_number_div_100($wra_detail['account_back_money']);
        $wra_detail['trademark_back_money'] = deal_number_div_100($wra_detail['trademark_back_money']);
        $wra_detail['science_money'] = deal_number_div_100($wra_detail['science_money']);
        $wra_detail['other_money'] = deal_number_div_100($wra_detail['other_money']);
        $wra_detail['today_total_money'] = deal_number_div_100($wra_detail['today_total_money']);
        $wra_detail['pic'] = !empty($wra_detail['pic']) ? json_decode($wra_detail['pic'], true) : [];

        //获取目前金额
        $now_money = $wrd->get_month_now_money($wra_detail['department'], $wra_detail['now_date']);
        $wra_detail['now_money'] = $now_money;

        //获取目标金额
        $target_money = $wrd->get_month_target_money($wra_detail['department'], $wra_detail['now_date']);
        $wra_detail['target_money'] = $target_money;

        $data['wra_detail'] = $wra_detail;
        return json($this->wrd_logic->get_response(1, 'OK', $data));
    }

    /**
     * 修改每日数据明细
     * @return \think\response\Json
     */
    public function ajax_save_wrd()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改每日数据明细规则
        $r = $this->wrd_logic->check_save_wrd_rule($params);
        if (!$r['code']) {
            return json($r);
        }
        $wrd_detail = $r['data']['wrd_detail'];

        $wcd_data = [];
        $wcd_data['department'] = trim($params['department']);
        $wcd_data['today_shop_person_num'] = trim($params['today_shop_person_num']);
        $wcd_data['front_money'] = deal_number_mul_100(trim($params['front_money']));
        $wcd_data['vip_back_money'] = deal_number_mul_100(trim($params['vip_back_money']));
        $wcd_data['check_in_back_money'] = deal_number_mul_100(trim($params['check_in_back_money']));
        $wcd_data['agency_back_money'] = deal_number_mul_100(trim($params['agency_back_money']));
        $wcd_data['account_back_money'] = deal_number_mul_100(trim($params['account_back_money']));
        $wcd_data['trademark_back_money'] = deal_number_mul_100(trim($params['trademark_back_money']));
        $wcd_data['science_money'] = deal_number_mul_100(trim($params['science_money']));
        $wcd_data['other_money'] = deal_number_mul_100(trim($params['other_money']));
        $wcd_data['today_total_money'] = deal_number_mul_100(trim($params['today_total_money']));
        $wcd_data['pic'] = json_encode($params['pic'], JSON_UNESCAPED_UNICODE);

        $maps = [];
        $maps[] = ['id', '=', $wrd_detail['id']];
        $r = model('work_record_data')->force()->save($wcd_data, $maps);
        if ($r !== false) {
            return json($this->wrd_logic->get_response(1, '修改成功'));
        }
        return json($this->wrd_logic->get_response(0, '修改失败'));
    }

    /**
     * 删除每日数据明细
     * @return \think\response\Json
     */
    public function ajax_del_wrd()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $r = model('work_record_data')->destroy($params['wrdid']);
        if ($r) {
            return json($this->wrd_logic->get_response(1, '删除成功'));
        }
        return json($this->wrd_logic->get_response(0, '删除失败'));
    }

    /**
     * 导出每日数据明细
     * http://127.0.0.1/mandiao-mini-api/public/admin-work_record_data-export_wrd?token=e916a6039bff505bacaf5def2bb8435f&page=1&rows=10&department=全部&username=&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_wrd()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //获取每日数据明细
        $r = $this->wrd_logic->get_wrd($params);
        $list = $r['data']['list'];

        $row = ['部门名称', '提交人', '今日到店客户数', '定金（元）', '会员回款（元）', '入驻回款（元）', '代办回款（元）', '代账回款（元）', '商标回款（元）', '科技服务（元）', '其他（元）', '目标金额（元）', '目前金额（元）', '完成率', '日期'];

        $export_data = [];
        foreach ($list as $k => $v) {
            $export_data[$k]['department'] = $v['department'];
            $export_data[$k]['username'] = $v['username'];
            $export_data[$k]['today_shop_person_num'] = $v['today_shop_person_num'];
            $export_data[$k]['front_money'] = $v['front_money'];
            $export_data[$k]['vip_back_money'] = $v['vip_back_money'];
            $export_data[$k]['check_in_back_money'] = $v['check_in_back_money'];
            $export_data[$k]['agency_back_money'] = $v['agency_back_money'];
            $export_data[$k]['account_back_money'] = $v['account_back_money'];
            $export_data[$k]['trademark_back_money'] = $v['trademark_back_money'];
            $export_data[$k]['science_money'] = $v['science_money'];
            $export_data[$k]['other_money'] = $v['other_money'];
            $export_data[$k]['target_money'] = $v['target_money'];
            $export_data[$k]['now_money'] = $v['now_money'];
            $finish_rate = $v['target_money'] > 0 ? ($v['now_money'] / $v['target_money']) : 0;
            $finish_rate = deal_number_mul_100($finish_rate);
            $finish_rate = round($finish_rate, 0);
            $export_data[$k]['finish_rate'] = $finish_rate . '%';
            $export_data[$k]['now_date'] = $v['now_date'];
        }
        array_unshift($export_data, $row);

        $file_name = '每日数据明细' . date('YmdHi') . '.xlsx';
        $r = $this->wrd_logic->gen_wrd_excel($file_name, $export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->wrd_logic->get_response(1, '导出成功'));
    }
}
